/*
 * Created on 2004-5-26 To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

package cn.com.yinhex.as.util;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author test To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Generation - Code and Comments
 */
public class DateUtil implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*
	 * 统一时间
	 */
	private Calendar cld = null;

	/**
	 * 构造函数，生成当前时间的日历
	 */
	public DateUtil()
	{
		cld = Calendar.getInstance();
	}

	/**
	 * 用时间戳构造
	 */
	public DateUtil(long unixtime)
	{
		cld = Calendar.getInstance();
		setUnixtime(unixtime);
	}

	/**
	 * 用日期字符串构造
	 */
	public DateUtil(String datetime)
	{
		cld = Calendar.getInstance();
		setDatetime(datetime);
	}

	/**
	 * 将时间戳植入
	 */
	public void set(long unixtime)
	{
		setUnixtime(unixtime);
	}

	/**
	 * 将日期字符串植入
	 */
	public void set(String datetime)
	{
		setDatetime(datetime);
	}

	public void set(int year, int month, int day)
	{
		cld.set(year, month - 1, day, 0, 0, 0);
	}

	public void set(int year, int month, int day, int hour, int minute,
			int second)
	{
		cld.set(year, month - 1, day, hour, minute, second);
	}

	/**
	 * 得到时间戳
	 * 
	 * @return
	 */
	public long getLongtime()
	{
		return cld.getTimeInMillis();
	}

	/**
	 * 得到日期时间字符串
	 * 
	 * @return
	 */
	public String getCurTime()
	{
		try
		{
			int dyear = cld.get(Calendar.YEAR);
			String syear = StringUtil.addzero(dyear, 2);
			int dmonth = cld.get(Calendar.MONTH) + 1;
			String smonth = StringUtil.addzero(dmonth, 2);
			int dday = cld.get(Calendar.DAY_OF_MONTH);
			String sday = StringUtil.addzero(dday, 2);
			int dhour = cld.get(Calendar.HOUR_OF_DAY);
			String shour = StringUtil.addzero(dhour, 2);
			int dminute = cld.get(Calendar.MINUTE);
			String sminute = StringUtil.addzero(dminute, 2);
			int dsecond = cld.get(Calendar.SECOND);
			String ssecond = StringUtil.addzero(dsecond, 2);
			String dtime = syear + "-" + smonth + "-" + sday + " " + shour
					+ ":" + sminute + ":" + ssecond;
			return dtime;
		}
		catch (Exception e)
		{
			return "";
		}
	}

	/**
	 * 得到日期字符串
	 * 
	 * @return
	 */
	public String getDate()
	{
		try
		{
			int dyear = cld.get(Calendar.YEAR);
			int dmonth = cld.get(Calendar.MONTH) + 1;
			int dday = cld.get(Calendar.DAY_OF_MONTH);
			String dtime = StringUtil.addzero(dyear, 4) + "-"
					+ StringUtil.addzero(dmonth, 2) + "-"
					+ StringUtil.addzero(dday, 2);
			return dtime;
		}
		catch (Exception e)
		{
			return "";
		}
	}

	/**
	 * 得到年份
	 * 
	 * @return
	 */
	public int getYear()
	{
		return cld.get(Calendar.YEAR);
	}

	/**
	 * 得到月份
	 * 
	 * @return
	 */
	public int getMonth()
	{
		return cld.get(Calendar.MONTH) + 1;
	}

	/**
	 * 得到日份
	 * 
	 * @return
	 */
	public int getDay()
	{
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 得到小时
	 * 
	 * @return
	 */
	public int getHour()
	{
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 得到分钟
	 * 
	 * @return
	 */
	public int getMinute()
	{
		return cld.get(Calendar.MINUTE);
	}

	/**
	 * 得到秒
	 * 
	 * @return
	 */
	public int getSecond()
	{
		return cld.get(Calendar.SECOND);
	}

	/**
	 * 得到毫秒
	 * 
	 * @return
	 */
	public int getMillisecond()
	{
		return cld.get(Calendar.MILLISECOND);
	}

	/**
	 * 得到星期几
	 * 
	 * @return
	 */
	public String getWeekday()
	{
		int weekday = cld.get(Calendar.DAY_OF_WEEK);
		switch (weekday)
		{
		case 1:
			return "日";
		case 2:
			return "一";
		case 3:
			return "二";
		case 4:
			return "三";
		case 5:
			return "四";
		case 6:
			return "五";
		case 7:
			return "六";
		}
		return "";
	}

	public Calendar getCalendar()
	{
		return cld;
	}

	/////////////////////
	//
	///Private-Functions
	//
	/////////////////////
	private void setUnixtime(long unixtime)
	{
		try
		{
			cld.setTimeInMillis(unixtime);
		}
		catch (Exception e)
		{
			System.out.println("error");
			cld = Calendar.getInstance();
		}
	}

	private void setDatetime(String datetime)
	{
		Pattern p = Pattern.compile("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\\s*");
		Matcher m = p.matcher(datetime);
		if (m.find())
		{
			int year = IntUtil.s2i(m.group(1));
			int month = IntUtil.s2i(m.group(2));
			int day = IntUtil.s2i(m.group(3));
			if (year > 0 && month > 0 && day > 0)
			{
				p = Pattern.compile("([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})");
				m = p.matcher(datetime);
				if (m.find())
				{
					int hour = IntUtil.s2i(m.group(1));
					int minute = IntUtil.s2i(m.group(2));
					int second = IntUtil.s2i(m.group(3));
					this.set(year, month, day, hour, minute, second);
					return;
				}
				this.set(year, month, day);
			}
		}
	}

	public String ParseTime(String datetime, String pattern)
	{
		try
		{
			String date = "";
			String time = "";
			if (datetime.indexOf(" ") > 0)
			{
				String[] datetimelit = datetime.split(" ");
				date = datetimelit[0];
				time = datetimelit[1];
			}
			else
			{
				date = datetime;
			}
			String[] datelit = date.split("-");
			if (datelit == null || datelit.length != 3)
			{
				return datetime;
			}
			pattern = StringUtil.replace(pattern, "Y", datelit[0]);
			pattern = StringUtil.replace(pattern, "M", datelit[1]);
			pattern = StringUtil.replace(pattern, "D", datelit[2]);
			String[] timelit = time.split(":");
			if (timelit == null || timelit.length != 3)
			{
				return pattern;
			}
			pattern = StringUtil.replace(pattern, "h", timelit[0]);
			pattern = StringUtil.replace(pattern, "m", timelit[1]);
			pattern = StringUtil.replace(pattern, "s", timelit[2]);
			return pattern;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return datetime;
		}
	}

	public String getNongli()
	{
		try
		{
			nongli nl = new nongli();
			Calendar cld = Calendar.getInstance();
			int year = cld.get(Calendar.YEAR);
			int month = cld.get(Calendar.MONTH) + 1;
			int day = cld.get(Calendar.DAY_OF_MONTH);
			long[] l = nl.calElement(year, month, day);
			String n = "";
			switch ((int) (l[1]))
			{
			case 1:
				n = "一";
				break;
			case 2:
				n = "二";
				break;
			case 3:
				n = "三";
				break;
			case 4:
				n = "四";
				break;
			case 5:
				n = "五";
				break;
			case 6:
				n = "六";
				break;
			case 7:
				n = "七";
				break;
			case 8:
				n = "八";
				break;
			case 9:
				n = "九";
				break;
			case 10:
				n = "十";
				break;
			case 11:
				n = "十一";
				break;
			case 12:
				n = "十二";
				break;
			}
			return n + "月" + nl.getchina((int) (l[2]));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 今天和某日的差日期
	 * @param date
	 * @return
	 */
	public int datediff(String date)
	{
		date = date.replaceAll("-", "");
		int year = Integer.parseInt(date.substring(0, 4));
		int month = Integer.parseInt(date.substring(4, 6));
		int day = Integer.parseInt(date.substring(6, 8));
		Calendar cld2 = Calendar.getInstance();
		cld2.set(year, month - 1, day);
		long difftime = cld.getTimeInMillis() - cld2.getTimeInMillis();
		int diffdate = (int) (difftime / 86400000L);
		return diffdate;
	}

	public int getDaynum()
	{
		return (int) (cld.getTimeInMillis() / 86400000);
	}

	public static long toLongTime(String gmtdate)
	{
		if (gmtdate == null)
		{
			return Long.MAX_VALUE;
		}
		try
		{
			return Date.parse(gmtdate);
		}
		catch (Exception e)
		{
			return System.currentTimeMillis();
		}
	}

	public Date getTime()
	{
		return cld.getTime();
	}

	public String toString()
	{
		return this.getCurTime();
	}
}
